package com.icemelon.catdog.service;

import com.icemelon.catdog.dao.UserDetailsMapper;
import com.icemelon.catdog.dao.UserMapper;
import com.icemelon.catdog.exception.MyException;
import com.icemelon.catdog.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserDetailsMapper userDetailsMapper;

    @Override
    public Integer CreateUser(String userName, String userPwd) {
        User user = new User();
        user.setUserName(userName).setUserPwd(userPwd);
        int insert = userMapper.insert(user);
        return insert;
    }

    @Override
    public User selectUser(String userName, String userPwd) throws MyException {
        User user = userMapper.selectUserByName(userName);
        if (user.getUserPwd().equals(userPwd))
            return user;
        throw new MyException("密码错误");
    }

    @Override
    public User updateUserPwd(String userName, String userOldpwd, String userNewpwd) throws MyException {
        User user = userMapper.selectUserByName(userName);
        if (user.getUserPwd().equals(userOldpwd)){
            //更新密码
            user.setUserPwd(userNewpwd);
            int i = userMapper.updateById(user);
            if (i>=0)
                return user;
        }
        throw new MyException("密码错误");
    }
}
